package com.ld.security.server.admin.modules.gl.vo;

import com.ld.security.common.mybatis.anno.*;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.server.admin.modules.gl.entity.GlEnumEntity;
import com.ld.security.server.admin.modules.gl.entity.GlEnumItemEntity;
import com.ld.security.common.core.base.SuperVo;
import com.ld.security.common.core.exception.ApiException;
import lombok.Data;

import java.util.List;

@Data
public class EnumDetailsVo implements SuperVo {

    private String code;

    @QueryKey
    @SaveKey
    @RemoveKey
    @SqlKey(name = "code",valueField = "code")
    private GlEnumEntity parent;

    @RemoveKey
    @SaveKey
    @QueryKey
    @SqlKey(name = "enumId",valueField = "parent.id",value = SqlCondition.in)
    @OrderBy(value = "sort_val")
    private List<GlEnumItemEntity> children;

    /**
     * 根据标题获得枚举
     * @param label
     * @return
     */
    public  GlEnumItemEntity findByLabel(String label){
        for (GlEnumItemEntity child : children) {
            if(child.getLabel().equals(label)){
                return child;
            }
        }
        ApiException.throw400("枚举不存在："+label);
        return null;
    }

    /**
     * 根据值获得枚举
     * @param value
     * @return
     */
    public  GlEnumItemEntity findByValue(String value){
        for (GlEnumItemEntity child : children) {
            if(child.getValue().equals(value)){
                return child;
            }
        }
        ApiException.throw400("枚举不存在："+value);
        return null;
    }

}
